#!/usr/bin/env bash

icon="/usr/share/icons/Faba/48x48/notifications/notification-keyboard-brightness.svg"

if [ "$1" == "-c" ]
then
        timing=$(wofi --width 300 --height 70 -i --dmenu --prompt "Clip time")
        start=$(echo $timing | awk '{print $1}') 
        stop=$(echo $timing | awk '{print $2}')

        if [ -z "$timing" ]
        then
                exit
        fi
 
        file_name=$(date +'%Y-%m-%d-%H%M%S.mp4') 

        dunstify "Downloading..." -i "$icon" -t 5000  
  
         yt-dlp  -S res,ext:mp4:m4a --recode mp4 --external-downloader ffmpeg --external-downloader-args \
                 "ffmpeg_i:-ss $start -to $stop" $(wl-paste) \
                 -o "$file_name" -P "/dev/shm"
        
         dunstify "Downloaded "$file_name"" -i "$icon" -t 5000
     
else  
        confirm_strings="cancel\nyt-dlp\n" 
        confirm=$(echo -e $confirm_strings | wofi --width 300 --height 150 -i --dmenu )
 
        dunstify "Downloading..." -i "$icon" -t 5000 
   
        "$confirm" -S res,ext:mp4:m4a --recode mp4 $(wl-paste) \
                 -o "$($file_name)" -P "/dev/shm"
    
         dunstify "Downloaded "$file_name"" -i "$icon" -t 5000
fi
